package jvm;

import java.io.*;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class HelloClassLoader extends ClassLoader {

    public static void main(String[] args) {
        try {
            Class clazz = new HelloClassLoader().findClass("Hello");
            Object instance = clazz.newInstance();
            Method helloMethod = clazz.getMethod("hello");
            helloMethod.invoke(instance);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] bytes = null;
        try {
            Path path = Paths.get("C:\\Users\\DELL\\Desktop\\geek_java\\jvm\\src\\jvm\\Hello.xlass");
            bytes = Files.readAllBytes(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] classBytes = new byte[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            classBytes[i] = (byte) (255 - bytes[i]);
        }
        return defineClass(name, classBytes, 0, classBytes.length);
    }
}
